//
//  TShareViewItem.m
//  Traceability
//
//  Created by ZhaoDongBo on 16/8/15.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "TShareViewItem.h"
#import "Masonry.h"

@implementation TShareViewItem

- (instancetype)initWithTitle:(NSString *)titleText andImage:(UIImage *)image {
    
    self = [super init];
    if (self) {
        
        [self setup];
        
        self.titleLabelText.text = titleText;
        self.titleImageView.image = image;
    }
    return self;
}

- (void)setup {
    [self addSubview:self.titleImageView];
    [self addSubview:self.titleLabelText];
    
    [self addAutoLayoutToView];
}

- (UIImageView *)titleImageView {
    if (_titleImageView == nil) {
        
        GET_SCREEN_WIDTH(screen_width);
        const float itemWidth = (screen_width-20-160.0)/4.0;
        
        _titleImageView = [[UIImageView alloc] init];
        _titleImageView.layer.masksToBounds = YES;
        _titleImageView.layer.cornerRadius  = itemWidth/2.0;
        
    }
    return _titleImageView;
}

- (UILabel *)titleLabelText {
    if (_titleLabelText == nil) {
        _titleLabelText = [[UILabel alloc] init];
        _titleLabelText.textColor       = UIColorFromRGB(0x666666,1);
        _titleLabelText.font            = [UIFont systemFontOfSize:12];
        _titleLabelText.textAlignment   = NSTextAlignmentCenter;
    }
    
    return _titleLabelText;
}

- (void)addAutoLayoutToView {
    
    WS(weakSelf);
    
    [_titleImageView makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.bottom.mas_equalTo(weakSelf.mas_bottom).with.offset(-20);
    }];
    
    [_titleLabelText makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.titleImageView.mas_bottom).with.offset(5);
        make.leading.mas_equalTo(-15);
        make.trailing.mas_equalTo(15);
        make.bottom.mas_equalTo(0);
    }];
}

@end
